package com.vpx.server.service.system.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.SysOperateLog;
import com.vpx.server.bean.info.base.UserInfo;
import com.vpx.server.bean.order.system.SysOperateLogOrder;
import com.vpx.server.bean.order.system.SysOperateLogPageOrder;
import com.vpx.server.bean.result.system.SysOperateLogPageResult;
import com.vpx.server.framework.aop.ServiceHandler;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.mapper.system.SysOperateLogMapper;
import com.vpx.server.service.system.SysOperateLogService;
import com.vpx.server.utils.UserLoginUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module: 操作日志服务
 * @description: 操作日志服务实现
 * @date 2021/09/13 09:58
 */
@Service
public class SysOperateLogServiceImpl implements SysOperateLogService {
    private final static Logger logger = LoggerFactory.getLogger(SysOperateLogServiceImpl.class);

    @Autowired
    private SysOperateLogMapper sysOperateLogMapper;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultMsg insert(SysOperateLogOrder order) {
        UserInfo userInfo = UserLoginUtils.getCurrentUser();
        if (userInfo != null) {
            Date now = DateUtils.now();
            SysOperateLog sysOperateLog = new SysOperateLog();
            sysOperateLog.from(order);
            sysOperateLog.setUserId(userInfo.getId());
            sysOperateLog.setLoginAccount(userInfo.getLoginAccount());
            sysOperateLog.setUserName(userInfo.getUserName());
            sysOperateLog.setCreateTime(now);
            sysOperateLog.setUpdateTime(now);
            sysOperateLog.setUpdateUserId(userInfo.getId());
            sysOperateLogMapper.insert(sysOperateLog);
        } else {
            logger.info("记录操作日志未登录 入参:" + JSON.toJSONString(order));
        }
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "分页查询操作日志")
    public ResultMsg<SysOperateLogPageResult> pageQuery(SysOperateLogPageOrder order) {
        if (order.getPageNum() != null && order.getPageSize() != null) {
            PageHelper.startPage(order.getPageNum(), order.getPageSize());
        }
        if(StringUtils.isNotBlank(order.getOperateDateStart())) {
            String beginTime = order.getOperateDateStart();
            order.setOperateDateStart(DateUtil.parse(beginTime, DatePattern.NORM_DATETIME_PATTERN).getTime()+"");
        }
        if(StringUtils.isNotBlank(order.getOperateDateEnd())) {
            String endTime = order.getOperateDateEnd();
            order.setOperateDateEnd(DateUtil.parse(endTime, DatePattern.NORM_DATETIME_PATTERN).getTime()+"");
        }
        List<SysOperateLog> sysOperateLogs = sysOperateLogMapper.pageQuery(order);
        PageInfo<SysOperateLog> pageInfo = new PageInfo<SysOperateLog>(sysOperateLogs);
        SysOperateLogPageResult pageResult = new SysOperateLogPageResult();
        pageResult.setPageInfo(pageInfo);
        return ResultMsg.success(pageResult);
    }
}
